package com.util;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import com.bean.CommentsMessage;
import com.bean.UserLogin;
import com.bean.ZanMessage;

@Component
public class ZanUtil {

	@Autowired
	private MongoTemplate mongoTemplate;
	public List<CommentsMessage> getCommentsMessage(List<CommentsMessage> data,String userId){
		for(CommentsMessage commentsMessage:data){
			if(StringUtils.isNotEmpty(userId)){
				long count=mongoTemplate.count(new Query(Criteria.where("text_types").
						is(Domain.zan_message_text_types_comments).
						and("msg_id").is(commentsMessage.getId()).and("user_id").is(userId).and("is_delete").is(Domain.zan_message_is_delete_n)), ZanMessage.class);
				if(count>0){
					commentsMessage.setIsZan("true");
				}else{
					commentsMessage.setIsZan("false");
				}
			}else{
				commentsMessage.setIsZan("false");
			}
		}
		return data;
	}
	public List<ZanMessage> getZanMessage(List<ZanMessage> zanMessages){
		for(ZanMessage zanMessage:zanMessages){
			ObjectId objId=zanMessage.getObjId();
			CommentsMessage commentsMessage=mongoTemplate.findById(objId, CommentsMessage.class);
			zanMessage.setComments(commentsMessage.getContent());
			zanMessage.setCommentsMsgTypes(commentsMessage.getTable());
			//UserLogin userLogin=mongoTemplate.findById(zanMessage.getUser_id(), UserLogin.class);
			//zanMessage.setZan_avatar(commentsMessage.getUsers().getUsrImg());
			//zanMessage.setZan_nickname(commentsMessage.getUsers().getUsername());
		}
		return zanMessages;
	}
}
